package com.lc.cehui.ui.activity.mine;

import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.base.BaseDialog;
import com.lc.cehui.dialog.MenuDialog;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.BookDelApi;
import com.lc.cehui.http.request.BookStatusApi;
import com.lc.cehui.http.request.MyBookApi;
import com.lc.cehui.http.response.MyBookBean;
import com.lc.cehui.http.response.RegisterBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.util.SPUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class MyCertificateActivity extends AppActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private BaseQuickAdapter adapter;
    private  List<MyBookBean.DataBean> list = new ArrayList<>();
    private List<String> menulist = new ArrayList<>();
    private int id;
    private int status;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mycertificate;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter = new BaseQuickAdapter<MyBookBean.DataBean, BaseViewHolder>(R.layout.item_myresume,list) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, MyBookBean.DataBean dataBean) {
                baseViewHolder.setText(R.id.tv_title,dataBean.getTitle());
                baseViewHolder.setText(R.id.tv_date,dataBean.getCreate_time());
                GlideApp.with(MyCertificateActivity.this).load(dataBean.getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) baseViewHolder.getView(R.id.img)) ;
                baseViewHolder.getView(R.id.img_set).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        menulist.clear();
                        id = dataBean.getInfo_id();
                        status = dataBean.getStatus();
                        if (dataBean.getStatus()==0){
                            menulist.add("展示");
                        }else {
                            menulist.add("不展示");
                        }
                        menulist.add("删除");
                        showMenuDialog(menulist);
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {

        my_book();


    }

    private void my_book() {
        EasyHttp.post(this)
                .api(new MyBookApi().setUserid(SPUtil.get(getContext(), IntentKey.USERID,0)))
                .request(new HttpCallback<MyBookBean>(this) {

                    @Override
                    public void onSucceed(MyBookBean data) {

                        if (data.getCode().equals("200")) {
                            list.clear();
                            list.addAll(data.getData());
                            adapter.setNewData(list);
                            adapter.notifyDataSetChanged();
                        }

                    }
                });
    }


    private void showMenuDialog(List home_search) {
        new MenuDialog.Builder(getActivity())
                .setList(home_search)
                .setTitle("请选择操作")
                .setListener(new MenuDialog.OnListener<String>() {
                    @Override
                    public void onSelected(BaseDialog dialog, int position, String string) {
                        switch (position) {
                            case 0:
//                             展示

                                if (status==0){
                                    status=1;
                                }else {
                                    status=0;
                                }
                                my_book_status();
                                break;
                            case 1:
//                         删除

                                my_book_del();
                                break;

                        }
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {

                    }
                }).show();
    }

    private void my_book_del() {
        EasyHttp.post(this)
                .api(new BookDelApi().setUserid(SPUtil.get(getContext(), IntentKey.USERID,0)).setInfo_id(id))
                .request(new HttpCallback<RegisterBean>(this) {
                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMsg());
                        if (data.getCode().equals("200")) {
                            my_book();
                        }
                    }
                });
    }

    private void my_book_status() {
        EasyHttp.post(this)
                .api(new BookStatusApi().setUserid(SPUtil.get(getContext(), IntentKey.USERID,0)).setInfo_id(id).setStatus(status))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMsg());
                        if (data.getCode().equals("200")) {
                            my_book();
                        }
                    }
                });
    }

    @OnClick(R.id.bt_submit)
    public void onClick() {
        startActivity(SubmitCertificateActivity.class);
    }
}
